package com.cti.ftpservice.utils;

import java.util.ArrayList;
import java.util.List;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.encoding.ser.BeanDeserializerFactory;
import org.apache.axis.encoding.ser.BeanSerializerFactory;
import org.apache.axis.message.SOAPHeaderElement;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.cti.ftpservice.serviceImpl.FtpServiceImpl;
import com.ctibook.booksystem.ws.FtpFileDownloadViewVo;
import com.ctibook.booksystem.ws.FtpFileUploadInfoViewVo;
import com.ctibook.booksystem.ws.FtpServiceUploadFtpFilesViewVo;
import com.ctibook.booksystem.ws.FtpServiceViewVo;
import com.ctibook.booksystem.ws.FtpServiceWSProxy;
import com.ctibook.booksystem.ws.FtpUploadFileInfoRecordViewVo;

public class FtpWsManagerUtil {
	
	public static String namespace="http://ws.booksystem.ctibook.com";
	private static Log logger = LogFactory.getLog(FtpWsManagerUtil.class);
	public static SOAPHeaderElement getSOAPHeader() {
		SOAPHeaderElement header = new SOAPHeaderElement("","Author");  
		try {
		        header.setPrefix("");//前缀  
		        header.addChildElement("email").addTextNode(WsPropertiesLoadUtil.email);    
		        header.addChildElement("password").addTextNode(WsPropertiesLoadUtil.password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		  return header;   
	}
	
	 public static String  test(){
			try {
				Call call = (Call)new Service().createCall();
				call.setTargetEndpointAddress(WsPropertiesLoadUtil.url);  
		        call.addHeader(getSOAPHeader());  
		        call.addParameter(new QName("name"), XMLType.XSD_STRING, ParameterMode.IN);
		        call.setOperationName(new javax.xml.namespace.QName(namespace, "test"));
		        call.setReturnType(XMLType.XSD_STRING);
		        Object obj = call.invoke(new String[]{"测试"});  
		        return obj.toString();
			} catch (Exception e) {
				e.printStackTrace();
			}  
	        return null;
	}
	 

		@SuppressWarnings("unchecked")
		public static <T>  List<T> convertObjectToModelList(Object obj){
			List<T> vos=new ArrayList<T>();
	        if(obj!=null){
	        	 if (obj instanceof ArrayList<?>){
	        		 vos=(List<T>)obj;
	 	        }else{
	 	        	vos.add((T)obj);
	 	        }
	        }
	        return vos;
		}  

	/**
	 * 原生态的调用方式 
	 * @param ftpServiceType
	 * @return
	 */
	public  static List<FtpServiceViewVo> getFtpServiceViewVosByType(String ftpServiceType,int enableValue) {
		try {
			Call call = (Call)new Service().createCall();
			call.setTargetEndpointAddress(WsPropertiesLoadUtil.url);  
	        call.addHeader(getSOAPHeader()); 
	        call.addParameter("ftpServiceType",XMLType.XSD_STRING, ParameterMode.IN);  
	        call.addParameter("enableValue",XMLType.XSD_INT, ParameterMode.IN);  
	        QName retunTypeName=new QName(namespace, "ftpServiceViewVo");
	        call.registerTypeMapping(FtpServiceViewVo.class,retunTypeName,new BeanSerializerFactory(FtpServiceViewVo.class, retunTypeName),
	                new BeanDeserializerFactory(FtpServiceViewVo.class,retunTypeName));
	        call.setReturnType(retunTypeName);
	        call.setOperationName(new javax.xml.namespace.QName(namespace, "getFtpServiceViewVosByType"));
	        Object obj = call.invoke(new Object[]{ftpServiceType,enableValue});
	        return convertObjectToModelList(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}  
        return null;
	}
	
	/**
	 * 代理调用
	 * @param ftpServiceType
	 * @return
	 */
	public  static FtpServiceViewVo[] getFtpServiceViewVosByTypeFromProxy(String ftpServiceType) {
		try {
			 FtpServiceWSProxy ftp=new FtpServiceWSProxy();
    		 return ftp.getFtpServiceViewVosByType(ftpServiceType,1);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}  
        return null;
	}
	
	public static void deleteFtpService(String ftpServiceId) {
		try {
			FtpServiceWSProxy ftp=new FtpServiceWSProxy();
			 ftp.deleteFtpService(ftpServiceId);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}  
	}
	
	public static void  addOrUpdateFtpService(FtpServiceViewVo viewVo) {
		try {
			 FtpServiceWSProxy ftp=new FtpServiceWSProxy();
			 ftp.addOrUpdateFtpService(viewVo);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}  
	}
	
	/**
	 * 
	 * @return
	 */
	public  static FtpServiceUploadFtpFilesViewVo[] getFtpServiceUploadFtpFileInfo() {
		try {
			 FtpServiceWSProxy ftp=new FtpServiceWSProxy();
    		 return ftp.getFtpServiceUploadFtpFileInfo();
		} catch (Exception e) {
			logger.error(e.getMessage());
		}  
        return null;
	}
	
	public  static void  addUploadFtpFileRecord(FtpFileUploadInfoViewVo viewVo,String ftpServiceId) {
		try {
			 FtpServiceWSProxy ftp=new FtpServiceWSProxy();
			 FtpUploadFileInfoRecordViewVo recordVo=convertFtpServiceUploadFtpFilesViewVoToUploadFtpFileRecord(viewVo);
    		 ftp.addUploadFtpFileRecord(recordVo,ftpServiceId);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}  
	}
	
	public static FtpUploadFileInfoRecordViewVo convertFtpServiceUploadFtpFilesViewVoToUploadFtpFileRecord(FtpFileUploadInfoViewVo viewVo){
		FtpUploadFileInfoRecordViewVo recordVo=new FtpUploadFileInfoRecordViewVo();
		if(viewVo!=null)
		{
			recordVo.setFtpFileInfoId(viewVo.getId());
			recordVo.setUploaderId(viewVo.getUploaderId());
			recordVo.setUploadFtpServiceId(viewVo.getFtpServiceId());
		}
		return recordVo;
	} 
	
	
	public static String[] getFtpFileMatchFtpServiceRecordIdsByFtpFileIdAndFtpServiceId(String ftpFileId,String ftpServiceId){
		try {
			 FtpServiceWSProxy ftp=new FtpServiceWSProxy();
			 return ftp.getFtpFileMatchFtpServiceRecordIdsByFtpFileIdAndFtpServiceId(ftpFileId,ftpServiceId);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return null;
	}
	
	
	public static void updateFtpFileMatchFtpServiceRecordPassInfo(String recordId,Boolean pass){
		try {
			 FtpServiceWSProxy ftp=new FtpServiceWSProxy();
			 ftp.updateFtpFileMatchFtpServiceRecordPassInfo(recordId,pass);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
	}

	
	public static FtpFileDownloadViewVo[] getFtpDownloadFileViewVoByFtpServiceIdAndDownloadPath(String ftpServiceId,String[] downloadPaths){
		try {
			 FtpServiceWSProxy ftp=new FtpServiceWSProxy();
			 return ftp.getFtpDownloadFileViewVoByFtpServiceIdAndDownloadPath(ftpServiceId, downloadPaths);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return null;
	}
	
}
